﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace GesturePlayer.SongLibrary
{
    class SongLibraryFetcher
    {
        public string Folder { get; set; }
        public List<Artist> Artists { get; private set; }

        public void FetchAllSongs()
        {
            Artists = new List<Artist>();

            foreach (string artistFolder in Directory.GetDirectories(Folder))
            {
                Artist artist = new Artist();
                artist.Albums = new List<Album>();
                artist.Name = Path.GetFileName(artistFolder);

                foreach (string albumFolder in Directory.GetDirectories(artistFolder))
                {
                    List<Title> titles = new List<Title>();

                    string coverPath = Path.Combine(albumFolder, "folder.jpg");

                    Album album = new Album();
                    album.Titles = new List<Title>();
                    album.Artist = artist;
                    album.CoverPath = File.Exists(coverPath) ? coverPath : null;
                    album.Name = Path.GetFileName(albumFolder);

                    int position = 0;

                    foreach (string titleFile in Directory.GetFiles(albumFolder, "*.mp3"))
                    {
                        string titleName = Path.GetFileNameWithoutExtension(titleFile);

                        List<string> splittedTitleName = new List<string>(titleName.Split('_'));
                        splittedTitleName.RemoveAt(0);
                        titleName = string.Join("_", splittedTitleName.ToArray());

                        Title title = new Title();
                        title.Album = album;
                        title.Position = ++position;
                        title.Name = titleName;
                        title.FileName = Path.Combine(albumFolder, titleFile);

                        album.Titles.Add(title);
                    }

                    artist.Albums.Add(album);
                }

                Artists.Add(artist);
            }
        }

        
    }
}
